import logging
import unittest
import requests
from common.db_util import DBUtil
from api.emp_api import EmpApi
from api.login_api import LoginApi
from config import BODY


class TestEmp(unittest.TestCase):
    mobile = "13344229062"
    au_headers = None

    @classmethod
    def setUpClass(cls) -> None:
        loginapi = LoginApi()
        # 从配置中获取成功登录的请求体
        body = BODY
        cls.au_headers = loginapi.get_au_headers(body)
        print(f"au_headers={cls.au_headers}")
        logging.info(f"au_headers={cls.au_headers}")

        sql = f"delete from bs_user where mobile='{cls.mobile}'"
        n = DBUtil.iud_data(sql)
        print(f"【添加员工前】，删除员工影响的行数为{n}")
        logging.info(f"【添加员工前】，删除员工影响的行数为{n}")

    @classmethod
    def tearDownClass(cls) -> None:
        sql = f"delete from bs_user where mobile='{cls.mobile}'"
        n = DBUtil.iud_data(sql)
        print(f"结束后，删除员工影响的行数为{n}")
        logging.info(f"结束后，删除员工影响的行数为{n}")

    def test_emp(self):
        empapi = EmpApi()
        body = {"mobile": self.mobile,
                "username": "大头儿子",
                "timeOfEntry": "2021-11-01",
                "workNumber": "123345555666",
                "departmentId": "1452451023720382464",
                "correctionTime": "2021-11-29T16:00:00.000Z"}
        resp = empapi.add_emp(body, self.au_headers)
        json_data = resp.json()
        print(f"【增加员工】的结果为{json_data}")
        logging.info(f"【增加员工】的结果为{json_data}")
        self.assertEqual(200, resp.status_code)
        self.assertEqual(True, json_data.get("success"))
        self.assertEqual(10000, json_data.get("code"))
        self.assertIn("成功", json_data.get("message"))

        emp_id = json_data.get("data").get("id")
        resp = empapi.query_emp(emp_id, self.au_headers)
        json_data = resp.json()
        print(f"【查询员工】的结果为{json_data}")
        logging.info(f"【查询员工】的结果为{json_data}")

        self.assertEqual(200, resp.status_code)
        self.assertEqual(True, json_data.get("success"))
        self.assertEqual(10000, json_data.get("code"))
        self.assertIn("成功", json_data.get("message"))

        body = {"username": "今天是周日"}
        resp = empapi.modify_emp(body, emp_id, self.au_headers)
        json_data = resp.json()
        print(f"【修改员工】的结果为{json_data}")
        logging.info(f"【修改员工】的结果为{json_data}")

        self.assertEqual(200, resp.status_code)
        self.assertEqual(True, json_data.get("success"))
        self.assertEqual(10000, json_data.get("code"))
        self.assertIn("成功", json_data.get("message"))

        resp = empapi.delete_emp(emp_id, self.au_headers)
        json_data = resp.json()
        print(f"【删除员工】的结果为{json_data}")
        logging.info(f"【删除员工】的结果为{json_data}")

        self.assertEqual(200, resp.status_code)
        self.assertEqual(True, json_data.get("success"))
        self.assertEqual(10000, json_data.get("code"))
        self.assertIn("成功", json_data.get("message"))
